﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;

namespace DataAccess
{
    public class ProductData
    {
        public IEnumerable<Product> AllProduct()
        {
            ShoppingCardDataContext shop = new ShoppingCardDataContext();
            IEnumerable<Product> list = from p in shop.Products
                                        where p.isActive == true
                                        select p;
            return list;         
        }

        public IEnumerable<Product> getAllProduct(int currpage, int recordpage, int pageSize, string query) {
            ShoppingCardDataContext shop = new ShoppingCardDataContext();
            IEnumerable<Product> list = (IEnumerable<Product>)(shop.spTB_Product_PhanTrang(currpage, recordpage, pageSize, query));
            return list;
        }

        public IEnumerable<Product> SearchProductByName(string name)
        {
            ShoppingCardDataContext shop = new ShoppingCardDataContext();
            IEnumerable<Product> list = shop.Products.Where(p => p.ProductName.Contains(name));
            return list;
        }

        public IEnumerable<Product> SearchProductByPrice(int bPrice, int ePrice)
        {
            ShoppingCardDataContext shop = new ShoppingCardDataContext();
            IEnumerable<Product> list = shop.Products.Where(p => p.ProductPrice >= bPrice && p.ProductPrice <= ePrice);
            return list;
        }

        public IEnumerable<Product> SearchProductByQuickSearch(string sText)
        {
            string text = "%" + sText + "%";
            ShoppingCardDataContext shop = new ShoppingCardDataContext();
            IEnumerable<Product> list = from p in shop.Products
                                        where p.ProductName == text
                                        select p;
            return list;
        }

        public Product GetProductById(int id)
        {
            ShoppingCardDataContext shop = new ShoppingCardDataContext();
            Product pr = shop.Products.Single(p => p.ProductID == id);
            return pr;
        }

        public IEnumerable<Product> getProductByCategory(int cid)
        {
            ShoppingCardDataContext ctx = new ShoppingCardDataContext();
            IEnumerable<Product> list = ctx.Products.Where(p => p.CategoryID == cid);
            return list;
        }

        public IEnumerable<Product> getProductBySupplier(int sid)
        {
            ShoppingCardDataContext ctx = new ShoppingCardDataContext();
            IEnumerable<Product> list = ctx.Products.Where(p => p.SupplierID == sid);
            return list;
        }

        public bool AddNewProduct(Product pr)
        {
            try
            {
                ShoppingCardDataContext shop = new ShoppingCardDataContext();
                shop.Products.InsertOnSubmit(pr);
                shop.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                
                return false;
            }
            
        }

        public void DeleteProductById(int id)
        {
            ShoppingCardDataContext shop = new ShoppingCardDataContext();
            Product pr = shop.Products.First(p => p.ProductID == id);
            pr.isActive = false;
            shop.SubmitChanges();
        }

        public void EditProduct(Product pro)
        {
            ShoppingCardDataContext shop = new ShoppingCardDataContext();
            Product pr = shop.Products.Single(p => p.ProductID == pro.ProductID);
            pr.ProductName = pro.ProductName;
            pr.ProductPrice = pro.ProductPrice;
            pr.Quantity = pro.Quantity;
            pr.SupplierID = pro.SupplierID;
            pr.CategoryID = pro.CategoryID;
            pr.imageLink = pro.imageLink;
            pr.Unit = pro.Unit;
            shop.SubmitChanges();
        }
    }
}
